package cn.ithers.blog.controller.portal;

import cn.ithers.blog.response.ResponseResult;
import cn.ithers.blog.service.IQrCodeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author: zhanghuan
 * @date: 2020/10/20 10:30
 * @description:
 */
@Slf4j
@Api(value = "二维码", tags = "二维码")
@RestController
@RequestMapping("/portal")
public class QrCodeApi {

    @Autowired
    private IQrCodeService iQrCodeService;

    /**
     * 获取二维码的请求路径
     * 二维码的图片路径
     * 二维码的code
     *
     * @return
     */
    @ApiOperation("获取二维码的请求路径")
    @GetMapping("/pc_login_qr_code")
    public ResponseResult getPcQrCode() {
        //1.生成唯一的id
        //2.保存到redis,值为false，时间为5分钟（二维码的有效期）
        //3.返回结果
        return iQrCodeService.getPcQrCodeInfo();

    }

    /**
     * 改变登录状态接口
     *
     * @param loginId
     * @return
     */
    @ApiOperation("改变登录状态接口")
    @PutMapping("/qr_code_state/{loginId}")
    public ResponseResult updateQrCodeLoginState(@ApiParam(value = "登录ID",required = true) @PathVariable("loginId") String loginId) {
        return iQrCodeService.updateQrCodeLoginState(loginId);
    }

    /**
     * 检查二维码的登录状态
     *
     * @return
     */
    @ApiOperation("检查二维码的登录状态")
    @GetMapping("/qr_code_state/{loginId}")
    public ResponseResult checkQrCodeLoginState(@ApiParam(value = "登录ID",required = true) @PathVariable("loginId") String loginId) {
        return iQrCodeService.checkQrCodeLoginState(loginId);
    }

    /**
     * 给第三方扫描下载app用的接口
     * http://localhost:8080/portal/app/===768088956859318272
     */
    @ApiOperation("给第三方扫描下载app用的接口")
    @GetMapping("/app/{code}")
    public ResponseResult doloadAppForThirdPartScan(@ApiParam(value = "二维码code",required = true) @PathVariable("code") String code,
                                                    HttpServletRequest request,
                                                    HttpServletResponse response) {
        //todo:直接下载
        return ResponseResult.SUCCESS("移动端正在开发中......");
    }
}
